package com.example.batalhanaval;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RegistroActivity extends Activity implements OnClickListener{

	EditText edtNome, edtLogin, edtSenha;
	TextView txtNome, txtLogin, txtSenha;
	Button btnReg, btnCancel;

	RepositorioPlayer repositorioPlayer;
	Player player = new Player();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_registro);

		repositorioPlayer = new RepositorioPlayer(this);

		edtNome = (EditText)findViewById(R.id.editTextNomeReg);
		edtLogin = (EditText)findViewById(R.id.editTextLoginReg);
		edtSenha = (EditText)findViewById(R.id.editTextPwReg);
		txtNome = (TextView)findViewById(R.id.nomeReg);
		txtLogin = (TextView)findViewById(R.id.loginReg);
		txtSenha = (TextView)findViewById(R.id.senhaReg);

		btnReg = (Button)findViewById(R.id.btnRegOk);
		btnReg.setOnClickListener(this);
		btnCancel = (Button)findViewById(R.id.btnCancelReg);
		btnCancel.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.registro, menu);
		return true;
	}

	@Override
	public void onClick(View v) {

		String nome = edtNome.getText().toString();
		String login = edtLogin.getText().toString();
		String senha = edtSenha.getText().toString();

		switch(v.getId())
		{
		case R.id.btnRegOk:

			if(nome.equals(""))
			{
				Toast.makeText(this, R.string.msgNomeFieldEmpty, Toast.LENGTH_SHORT).show();
			}
			else if (login.equals(""))
			{
				Toast.makeText(this, R.string.msgLoginFieldEmpty, Toast.LENGTH_SHORT).show();

			}
			else if (senha.equals(""))
			{
				Toast.makeText(this, R.string.msgSenhaFieldEmpty, Toast.LENGTH_SHORT).show();
			}
			else
			{
				player.setNome(nome);
				player.setLogin(login);
				player.setSenha(senha);
				repositorioPlayer.inserir(player);
				finish();
			}
			
			break;
			
		case R.id.btnCancelReg:
		
		finish();
		break;

		}


	}

}
